#include "Timer.h"
#include <iostream>
#include <SDL.h>

Timer::Timer() : 
    m_dt(0), 
    m_oldTicks(0), 
    m_frameRate(0), 
    m_frames(0),
    m_timeKeep(0),
    m_printing(false)
{}
void Timer::Update()
{
	unsigned int newTicks = SDL_GetTicks(); //milliseconds
	unsigned int diff = newTicks - m_oldTicks;
    if(diff > 1000)
        diff = 16;
	m_oldTicks = newTicks;
	m_dt = diff;
    CalculateFrameRate();
}
const int Timer::GetDtSeconds() const 
{
	float res = (float)m_dt/1000.0f;
    return (int)res;
}
const int Timer::GetDtMilliSeconds() const
{
    return m_dt;
}
unsigned int Timer::GetElapsedTime()
{
	return m_oldTicks;
}
const int Timer::GetFrameRate()
{   
    return m_frameRate;
}
void Timer::CalculateFrameRate()
{
    m_frames++;
    float nr = (float)m_dt / 1000.0f;
    m_timeKeep +=  (int)nr;
    if ( m_timeKeep > 1)
    {
        m_frameRate = m_frames;
        m_timeKeep = 0;		
        m_frames = 0;
    }
}
void Timer::TogglePrint()
{
    m_printing = !m_printing;
}